import request from './index'

// 添加订单
export const addOrder = (data) => {
  console.log('发送订单数据:', data)
  return request({
    url: '/order/add',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data
  })
}

// 获取订单列表
export const getOrderList = () => {
  return request({
    url: '/order/list',
    method: 'get'
  })
}

// 获取订单详情
export const getOrderDetail = (id) => {
  return request({
    url: '/order/detail',
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: `id=${id}`
  })
}

// 支付订单
export const payOrder = (orderNo, actualAmount) => {
  console.log('发送支付请求，订单号:', orderNo, '金额:', actualAmount);
  return request({
    url: '/api/alipay/pay',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data: {
      orderNo: orderNo,
      actualAmount: actualAmount
    }
  })
}

// // 支付成功回调，后端验证成功后直接重定向到支付成功页面，前端无需调用
// export const paySuccess = (orderNo) => {
//   return request({
//     url: '/api/alipay/toSuccess',
//     method: 'get',
//     params: {
//       orderNo: orderNo
//     }
//   })
// }

// 取消订单
export const cancelOrder = (id) => {
  return request({
    url: '/order/cancel',
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: `id=${id}`
  })
}

// 确认收货
export const confirmOrder = (id) => {
  return request({
    url: '/order/confirm',
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: `id=${id}`
  })
}

// 退货
export const returnOrder = (id) => {
  return request({
    url: '/order/return',
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: `id=${id}`
  })
}

// 发货
export const deliverOrder = (id) => {
  return request({
    url: '/order/deliver',
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: `id=${id}`
  })
}

// 评论订单
export const commentOrder = (id) => {
  return request({
    url: '/order/comment',
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: `id=${id}`
  })
}

// 获取所有已评论订单列表
export const getAllCommentedOrders = () => {
  return request.get('/order/allList')
} 